home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Devices and Hardware / Disks / FormatAsDOS / FormatAsDosDisk.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  4.2 KB  |  134 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        FormatAsDOSDisk.c
  3.     
  4.     Description:This snippet demonstrates how to use the newer Disk Initialization
  5.                 Package calls DIXFormat() and DIXZero() to format a floppy disk as
  6.                 a DOS disk.  DIXFormat, DIXZero, and DIReformat are documented in 
  7.                 the IM:Files Errata Tech Note.
  8.     
  9.                 Please note this snippet does not provide any prompts.  It merely takes
  10.                 the floppy in the first floppy drive and reformats it as a DOS disk with 
  11.                 the name "MS-DOS Disk".  This purpose of this snippet is to demonstrate
  12.                 the newer API.
  13.  
  14.     Author:        JL & VMC
  15.  
  16.     Copyright:     Copyright: © 1996-1999 by Apple Computer, Inc.
  17.                 all rights reserved.
  18.     
  19.     Disclaimer:    You may incorporate this sample code into your applications without
  20.                 restriction, though the sample code has been provided "AS IS" and the
  21.                 responsibility for its operation is 100% yours.  However, what you are
  22.                 not permitted to do is to redistribute the source as "DSC Sample Code"
  23.                 after having made changes. If you're going to re-distribute the source,
  24.                 we require that you make it clear in the source that the code was
  25.                 descended from Apple Sample Code, but that you've made changes.
  26.     
  27.     Change History (most recent first):
  28.                 6/24/99    Updated for Metrowerks Codewarror Pro 2.1(KG)
  29.  
  30. */
  31.  
  32. #include <Gestalt.h>
  33. #include <FSM.h>
  34. #include <DiskInit.h>
  35.  
  36. /******************************************************************************/
  37.  
  38. /*
  39. **    Prototypes and inlines for newer Disk INIT calls
  40. */
  41.  
  42. extern pascal OSErr DIXFormat(short drvNum, Boolean fmtFlag, unsigned long fmtArg, 
  43.                           unsigned long *actSize)
  44.         THREEWORDINLINE(0x700C, 0x3F00, 0xA9E9);
  45.  
  46. extern pascal OSErr DIXZero(short drvNum, ConstStr255Param volName, short fsid,
  47.                         short mediaStatus, short volTypeSelector,
  48.                         unsigned long volSize, void *extendedInfoPtr)
  49.         THREEWORDINLINE(0x700E, 0x3F00, 0xA9E9);
  50.  
  51. extern pascal OSErr DIReformat(short drvNum, short fsid, ConstStr255Param volName,
  52.                            ConstStr255Param msgText)
  53.         THREEWORDINLINE(0x7010, 0x3F00, 0xA9E9);
  54.  
  55. /*
  56. **    Prototypes for functions here
  57. */
  58.  
  59. Boolean  HasExtendedDIFunctions(void);
  60. OSErr    FormatAsDOS(short driveNumber, ConstStr255Param volName);
  61.  
  62.  
  63. /******************************************************************************/
  64.  
  65. /*
  66. **    See if new extended Disk Init package is available
  67. */
  68. Boolean  HasExtendedDIFunctions(void)
  69. {
  70.     long response;
  71.     
  72.     if ( Gestalt(gestaltFSAttr, &response) == noErr )
  73.         return ( (response & (1L << gestaltHasExtendedDiskInit)) != 0 );
  74.     else
  75.         return ( false );
  76. }
  77.  
  78. /******************************************************************************/
  79.  
  80. /*
  81. **    Format the unmounted floppy disk specified by driveNum as an MS-DOS disk.
  82. */
  83. OSErr    FormatAsDOS(short driveNumber, ConstStr255Param volName)
  84. {
  85.     enum
  86.     {
  87.         kMacPCExchangeFSID = 0x4953    /* file system ID of Macintosh PC Exchange's
  88.                                         MS-DOS file system (see "Guide-File System Mgr" 
  89.                                         on MacOS SDK in File System Manager SDK).*/
  90.     };
  91.     OSErr            result;
  92.     unsigned long    actSize;
  93.     
  94.     /*
  95.     **    Format the disk. Passing false for fmtFlag and 1440 for fmtArg tells
  96.     **    the Disk Init package to format the disk with 1440 blocks (720K MFM) or
  97.     **    if 1440 blocks isn't available (because the disk is a high density disk),
  98.     **    to format the disk with the smallest size that's greater than 1440 blocks
  99.     **    (which will be 2880 blocks (1440K MFM)).
  100.     */
  101.     result = DIXFormat(driveNumber, false, 1440, &actSize);
  102.     if ( result == noErr )
  103.     {
  104.         /* Verify the disk */
  105.         result = DIVerify(driveNumber);
  106.         
  107.         /*
  108.         **    The result of DIVerify is passed to DIXZero in the mediaStatus parameter.
  109.         **    If mediaStatus is noErr, then the disk is simply initialized.  If
  110.         **    mediaStatus is not noErr, then DIXZero attempts to spare any bad blocks
  111.         **    if the file system supports that. MS-DOS disks can be bad block spared.
  112.         */
  113.         result = DIXZero(driveNumber, volName, kMacPCExchangeFSID, result,
  114.                          0, actSize, NULL);
  115.     }
  116.     
  117.     return ( result );
  118. }
  119.  
  120. /******************************************************************************/
  121.  
  122. /* Test code */
  123. void    main(void)
  124. {
  125.     short            driveNumber = 1;    /* hard coded to use the 1st floppy drive */
  126.     OSErr            result;
  127.     Str255            volName = "\pMS-DOS Disk";
  128.     
  129.     if ( HasExtendedDIFunctions() )
  130.     {
  131.         result = UnmountVol(NULL, driveNumber);    /* unmount disk in the drive (if it was mounted) */
  132.         result = FormatAsDOS(driveNumber, volName);
  133.     }
  134. }